import {expect} from 'chai'
import {GolangTestParser} from '../../../src/parsers/golang-test/golang-test-parser'
import {readFile} from '../../../src/utils'
import {Coverage} from '../../../src/test-parser'

describe('count', () => {
    it('should correctly calculate coverage from the jsonl file', async () => {
        const jsonFile = await readFile('./test/parsers/golang-test/golang-test.jsonl')
        const expectedCoverage: Coverage = {
            total: 1,
            passed: 0,
            failed: 1,
            skipped: 0,
            passedRate: 0
        }

        const golangTestParser = new GolangTestParser()
        const res = await golangTestParser.count('./test/parsers/golang-test/golang-test.jsonl', jsonFile)
        expect(res).to.deep.equal(expectedCoverage)
    })
})

describe('File Path Converters', () => {
    describe('coverageFile', () => {
        it('should throw an error if the path does not end with .jsonl', async () => {
            try {
                const invalidPath = 'path/to/report.html'
                const golangParser = new GolangTestParser()
                await golangParser.coverageFile(invalidPath)
                expect.fail('Expected error was not thrown')
            } catch (error) {
                expect(error).to.be.an('error')
                // @ts-ignore
                expect(error.message).to.equal(`Input variable 'reportPath' should reference a report with jsonl extension`)
            }
        })
        it('should return the correct path if it ends with .jsonl', async () => {
            const validPath = './test/parsers/golang-test/golang-test.jsonl'
            const golangParser = new GolangTestParser()
            const result = await golangParser.coverageFile(validPath)
            expect(result).to.equal(validPath)
        })
    })
    describe('htmlFile', () => {
        it('should return the correct path', () => {
            const path = './test/parsers/golang-test/golang-test.jsonl'
            const golangParser = new GolangTestParser()
            const result = golangParser.htmlFile(path)
            expect(result).to.equal('./test/parsers/golang-test/golang-test.html')
        })
    })
})